<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/购物车.css">
</head>
<body>
    <div id="app">
        <div class="shoppingBar">
            <h2>购物清单</h2>
            <table>
                <tbody>
                    <tr>
                        <th><input type="checkbox" title="全选"></th>
                        <th>商品名称</th>
                        <th>数量</th>
                        <th>单价（元）</th>
                        <th>金额（元）</th>
                        <th>操作</th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="1"></th>
                        <th class="content"><img src="../static/images/20201102150701.png" width="100px" height="100px">
                            <p>电视机 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                2
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            200
                        </th>
                        <th class="money">
                            400
                        </th>
                        <th><a href="javascript:;" class="glyphicon glyphicon-remove">删除
                            </a></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="2"></th>
                        <th class="content"><img src="../static/images/20201102152230.png" width="100px" height="100px">
                            <p>洗衣机 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            150
                        </th>
                        <th class="money">
                            150
                        </th>
                        <th><a href="javascript:;" class="glyphicon glyphicon-remove">删除
                            </a></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="3"></th>
                        <th class="content"><img src="../static/images/20201102150701.png" width="100px" height="100px">
                            <p>冰箱 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            100
                        </th>
                        <th class="money">
                            100
                        </th>
                        <th><a href="javascript:;" class="glyphicon glyphicon-remove">删除
                            </a></th>
                    </tr>
                </tbody>
            </table>
            <div class="shoppingBar-footer">
                <div class="manage"><span class="delAll">
                        删除所选商品
                    </span> <span class="return">
                        继续购物
                    </span></div> <button id="go" type="button">
                    去结算
                </button>
                <div class="buy"><span>
                        0
                    </span>
                    件商品总计:
                    <span>
                        ￥0
                    </span></div>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    /*
        1、商品数量的加减 可能需要运算总数量
        2、商品勾选 需要运算总数量
        3、商品删除 需要运算总数量
        4、全选 需要运算总数量
    */ 
    // 事件委托实现数量的加减
    document.querySelector('.shoppingBar').addEventListener('click',function(event){
        let e = event||window.event;
        // 获取到事件源中的文本内容 并且去掉空格
        let content = e.target.innerHTML.trim();
        let number,price;
        switch (content) {
            case '-':
                //获取到当前数量
                number= parseInt(e.target.nextElementSibling.innerHTML)
                if(number<=1){
                    alert('数量最少1件');
                    return false;
                }
                // 数量减1
                e.target.nextElementSibling.innerHTML = --number;
                //获取到单价
                price =parseFloat(e.target.parentNode.nextElementSibling.innerHTML);
                // 计算金额
                e.target.parentNode.nextElementSibling.nextElementSibling.innerHTML = price * number;
                total();
                break;
            case '+':
                //获取到当前数量
                number = parseInt(e.target.previousElementSibling.innerHTML);
                // 数量减1
                e.target.previousElementSibling.innerHTML = ++number;
                //获取到单价
                price =parseFloat(e.target.parentNode.nextElementSibling.innerHTML);
                // 计算金额
                e.target.parentNode.nextElementSibling.nextElementSibling.innerHTML = price * number;
                total();
                break;
            case '删除':
                e.target.parentNode.parentNode.remove();
                total();
                break;
            default:
                if(e.target.className == 'input'){
                    // 勾选操作
                    total();
                    // 是否选中全选
                    if(document.querySelectorAll('.input:checked').length ==document.querySelectorAll('.input').length){
                        document.querySelector('input').checked = true;
                    }else{
                        document.querySelector('input').checked = false;
                    }
                }
                break;
        }
    })
    // 计算总数量信息
    function total(){
        // 查找被选中的input标签
        let inputs = document.querySelectorAll('.input:checked');
        let totalNumber=0,totalMoney=0;
        for(let i =0;i<inputs.length;i++){
            // 获取到当前勾选input的上上级tr标签
            let patrentTr = inputs[i].parentNode.parentNode
            // 计算总数量
            totalNumber += parseInt(patrentTr.children[2].children[1].textContent);
            // 计算总的金额
            totalMoney += parseFloat(patrentTr.children[4].textContent);
        }
        document.querySelectorAll('.buy span')[0].textContent = totalNumber;
        document.querySelectorAll('.buy span')[1].textContent = '¥'+totalMoney;
    }
</script>